package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.pojo.Brand;
import com.zhentao.service.BrandService;

import com.zhentao.service.FileStorageService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @autho：周明震
 * @date 2025/7/1
 */
@RestController
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private FileStorageService fileStorageService;
    //添加品牌及上传logo
    @PostMapping("brandAdd")
    @SneakyThrows
    public String brandAdd(@RequestParam("file") MultipartFile file,@RequestParam String name,@RequestParam String description,@RequestParam Integer isDeleted){
        String filename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        //上传图片
        String url = fileStorageService.uploadImgFile(filename, inputStream);
        //封装品牌实体类
        Brand brand1 = new Brand();
        //修改日期
        brand1.setCreateTime(new Date());
        brand1.setUpdateTime(new Date());
        //修改品牌名称
        brand1.setName(name);
        //修改logo路径
        brand1.setLogo(url);
        //修改品牌描述
        brand1.setDescription(description);
        //是否启用
        brand1.setIsDeleted(isDeleted);
        //添加
        boolean save = brandService.save(brand1);
        if(save){
            return "添加成功";
        }else {
            return "添加失败";
        }
    }
    //删除品牌
    @DeleteMapping("brandDelete")
    public String brandDelete(@RequestParam Integer id){//需要参数 id（前端传过来）
        //通过id删除
        boolean b = brandService.removeById(id);
        if(b){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }
    //回显
    @PostMapping("brandUpdateShow")
    public Brand brandUpdateShow(@RequestParam Integer id){//需要参数 id（前端传过来）
        //通过id获取数据
        Brand brand = brandService.getById(id);
        //返回给前端
        return brand;
    }
    //修改
    @PostMapping("brandUpdate")
    @SneakyThrows
    public String brandUpdate(@RequestParam Integer id,@RequestParam("file") MultipartFile file
            ,@RequestParam(required = false) String name,@RequestParam(required = false) String description,@RequestParam(required = false) Integer isDeleted){//需要参数 id（前端传过来）
        //设置minio的账号密码及路径
        String filename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        //上传图片
        String url = fileStorageService.uploadImgFile(filename, inputStream);
        //封装品牌实体类
        Brand brand1 = brandService.getById(id);
        //修改修改日期
        brand1.setUpdateTime(new Date());
        //修改品牌名称
        brand1.setName(name);
        //修改logo路径
        brand1.setLogo(url);
        //修改品牌描述
        brand1.setDescription(description);
        //是否启用
        brand1.setIsDeleted(isDeleted);
        //添加
        boolean a = brandService.updateById(brand1);
        if(a){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }
    //查询/模糊查询
    @PostMapping("brandList")
    public List<Brand> brandList(@RequestParam(required = false) String name){
        if(name!=null){
            QueryWrapper<Brand> wrapper = new QueryWrapper<>();
            wrapper.like("name",name);
            //获取所有品牌
            List<Brand> list = brandService.list(wrapper);
            return list;
        } else {
            List<Brand> list = brandService.list();
            return list;
        }
    }
    //状态修改
    @PostMapping("brandStatus")
    public String brandStatus(@RequestParam Integer id){//需要参数 id（前端传过来）
        //通过id获取数据
        Brand brand = brandService.getById(id);
        if (brand.getIsDeleted()==0){
            brand.setIsDeleted(1);
            //修改
            brandService.updateById(brand);
            return "状态修改成功";
        }else{
            brand.setIsDeleted(0);
            brandService.updateById(brand);
            return "状态修改成功";
        }
    }
}
